package cn.hzking.gjmall.activitys.second;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fingdo.statelayout.StateLayout;

import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.adpters.home.QgListAdapter;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.bean.QgGoodListBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.GjCustomStateViewObserver;
import cn.hzking.gjmall.utils.OnClickEvent;
import cn.hzking.gjmall.utils.SwipeRefreshLayoutUtils;
import cn.hzking.gjmall.view.SimpleOptionView;

/**
 * 抢购列表页
 */
public class QgGoodListActiivty extends ColorStatuBarActivity {

    private final static String CATEGORYID = "categoryId";
    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.cate_price)
    TextView catePrice;
    @Bind(R.id.cate_price_image)
    ImageView catePriceImage;
    @Bind(R.id.cate_new)
    TextView cateNew;
    @Bind(R.id.cateall)
    TextView cateall;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;
    @Bind(R.id.refrsh)
    SwipeRefreshLayout refrsh;
    @Bind(R.id.statlayout)
    StateLayout statlayout;
    private int checkId = 0;//选择的排序类型 0表示综合 1表示价格 2表示最新
    private boolean priceDown = true;//true表示降序 ,false表示升序
    private final String up = "price_asc";//升序字段
    private final String down = "price_desc";//降序字段
    private final String newest = "newest";//最新字段
    private int currentPage = 1;
    private QgListAdapter cateGoryGoodListAdapter;
    private String qgId;//首页传过来的抢购id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_normal_good_list);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        simpleTileView.setTitle("抢购列表");
        Intent intent = getIntent();
        qgId = intent.getStringExtra("qgId");
        SwipeRefreshLayoutUtils.initRefreshLayout(refrsh);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerview.setLayoutManager(gridLayoutManager);
        cateGoryGoodListAdapter = new QgListAdapter(R.layout.item_goodlist, null);
        recyclerview.setAdapter(cateGoryGoodListAdapter);
        cateall.setSelected(true);
        getNetData(qgId, null, true, false, true, true, null, null, null);
        initListener();

    }

    private void initListener() {
        cateall.setOnClickListener(onClickEvent);
        catePrice.setOnClickListener(onClickEvent);
        cateNew.setOnClickListener(onClickEvent);
        //刷新
        refrsh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                selectState(false,true);
            }
        });

        //上拉加载更多
        cateGoryGoodListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                selectState(false,false);

            }
        });
    }

    public void getNetData(String keyword, final String sort, boolean isFristInit, boolean isRefrsh, boolean isShowDialog, boolean isCancel, View emptyView, View onNetView, View errorView) {
        HashMap<String, String> parms = new HashMap<>();
        if (checkId == 0) {//综合
            parms.put("activityId", keyword);
            parms.put("pageIndex", String.valueOf(currentPage));
        } else if (checkId >= 1) {//价格或者最新
            parms.put("activityId", keyword);
            parms.put("pageIndex", String.valueOf(currentPage));
            parms.put("sort", sort);
        }
        RetrofitFactory.getInstant().API()
                .getQgList(parms)
                .compose(this.<QgGoodListBean>setThread())
                .subscribe(new GjCustomStateViewObserver<QgGoodListBean>(this, statlayout,
                        refrsh, isFristInit, isRefrsh, isShowDialog, isCancel, emptyView, onNetView, errorView) {
                    @Override
                    protected void onBusinessSuccess(QgGoodListBean qgGoodListBean) throws Exception {
                        if (qgGoodListBean != null) {
                            List<QgGoodListBean.DataBean.ItemListBean> itemList = qgGoodListBean.getData().getItemList();
                            if (currentPage == 1) {
                                cateGoryGoodListAdapter.setNewData(itemList);
                                if (cateGoryGoodListAdapter.getData().size() == 0) {
                                    View view = View.inflate(mActivity,R.layout.layout_statelayout_custom_goodlist_empty,null);
                                    statlayout.showCustomView(view);
                                    return;
                                }
                            } else {
                                cateGoryGoodListAdapter.addData(itemList);
                            }
                            currentPage++;
                        }
                        int itemCount = cateGoryGoodListAdapter.getData().size();
                        if (qgGoodListBean.getData().getTotalRecord() == itemCount) {//数据加载完成
                            cateGoryGoodListAdapter.loadMoreEnd(true);

                        } else {
                            cateGoryGoodListAdapter.loadMoreComplete();
                        }

                    }

                    @Override
                    protected void onChildFailure(Throwable e, boolean isNetWorkError) throws Exception {

                    }


                });
    }


    //清除排序状态
    private void clearAllState() {
        cateall.setSelected(false);
        setPriceState(false);
        cateNew.setSelected(false);


    }

    //价格状态
    private void setPriceState(boolean isSelected) {
        catePrice.setSelected(isSelected);
        catePriceImage.setSelected(isSelected);
        if (priceDown) {
            catePriceImage.setEnabled(false);
        } else {
            catePriceImage.setEnabled(true);
        }

    }

    private void selectState(boolean isFristInit,boolean isRefrsh){
        if (checkId == 0) {
            getNetData(qgId, null, isFristInit, isRefrsh, false, true, null, null, null);
        }
        else if(checkId == 1){
            if(priceDown){
                getNetData(qgId, down, isFristInit, isRefrsh, false, true, null, null, null);
            }else {
                getNetData(qgId, up, isFristInit, isRefrsh, false, true, null, null, null);
            }
        }else if(checkId == 2){
            getNetData(qgId, newest, isFristInit, isRefrsh, false, true, null, null, null);
        }
    }

    OnClickEvent onClickEvent = new OnClickEvent() {
        @Override
        public void singleClick(View v) {
            switch (v.getId()) {
                case R.id.cateall://综合
                    clearAllState();
                    cateall.setSelected(true);
                    checkId = 0;
                    currentPage = 1;
                    getNetData(qgId, null, true, false, true, true, null, null, null);
                    break;
                case R.id.cate_price://价格
                    clearAllState();
                    setPriceState(true);
                    checkId = 1;
                    currentPage = 1;
                    if (priceDown) {//是降序的
                        priceDown = false;
                        setPriceState(true);
                        getNetData(qgId, up, true, false, true, true, null, null, null);
                    } else {
                        priceDown = true;
                        setPriceState(true);
                        getNetData(qgId, down, true, false, true, true, null, null, null);
                    }

                    break;
                case R.id.cate_new://最新
                    clearAllState();
                    currentPage =1;
                    cateNew.setSelected(true);
                    checkId = 2;
                    getNetData(qgId, newest, true, false, true, true, null, null, null);
                    break;
            }

        }
    };

}
